/**
* Event services
*/
app.factory("eventServices", function($http, toolsServices) {
	console.info("start to register eventServices");
	var events = [];
	var onlineevents = [];
	var personalevents = [];
	return {
		allEvents: function(callback) {
			console.log('eventServices - get all events');
			$http.post(app.servicebase, { "service" : "getEvents" }).
			success(function(data, status) 
			{
				console.info("getEvents - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				events = data.response;
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("getEvents - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		personnalEvents: function(callback, userId) {
			$http.post(app.servicebase, { "service" : "getPersonalEvents", "userId" : userId}).
			success(function(data, status) 
			{
				console.info("getPersonalEvents - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				personalevents = data.response;
				callback(personalevents);
			})
			.
			error(function(data, status) 
			{
				console.error("getPersonalEvents - call failed"); 	
				throw status + ' : ' + data;				
			});
		},
		onlineEvents: function(callback) {
			console.log('eventServices - get all online events');
			$http.post(app.servicebase, { "service" : "getOnlineEvents" }).
			success(function(data, status) 
			{
				console.info("getOnlineEvents - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				onlineevents = data.response;
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("getOnlineEvents - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		getMainEvent: function(eventsUI){
			var mainEvent = undefined;
			for(var i = 0; i < eventsUI.length; i++) {
				//Compute the nearest date from  today
				var event = eventsUI[i];
				var today = new Date();
				if(today < event.DALObject.StartDate && event.DALObject.State != 6) {
					event.isMainEvent = true;
					mainEvent = event;
					break;
				}
			}
			
			if(mainEvent == undefined) {
				mainEvent = eventsUI[eventsUI.length-1];
				mainEvent.setIsMainEvent();
			}
			return mainEvent;
		},
		getIndexMainEvent: function(eventUI, eventsUI) {
			var result = -1;
			for(var i = 0; i < eventsUI.length; i++) {
				//Compute the nearest date from  today
				var event = eventsUI[i];
				if(event.DALObject.Id == eventUI.DALObject.Id) {
					result = i;
					break;
				}
			}
			return result;
		},
		subscribeOnEvent: function(callback, event, user) {
			console.log('eventServices - get all online events');
			$http.post(app.servicebase, { "service" : "preSubscribe", "userId" : user.Id, "eventId" : event.Id }).
			success(function(data, status) 
			{
				console.info("subscribeOnEvent - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				callback(data);
			})
			.
			error(function(data, status) 
			{
				console.error("subscribeOnEvent - call failed"); 	
				throw status + ' : ' + data;		
			});
		}
	};
});